package net.bcxuexi.server.stream;

import java.io.InputStream;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.List;

import net.bcxuexi.config.Config;
import net.bcxuexi.exception.MyException;
import net.bcxuexi.server.model.SocketModel;
import net.bcxuexi.server.stream.inhandle.ControlInputStreamHandler;
import net.bcxuexi.server.stream.inhandle.DataInputStreamHandlerServer;
import net.bcxuexi.server.stream.inhandle.ProxyInputStreamHandler;
import net.bcxuexi.server.stream.inhandle.ReadStreamHandler;
import net.bcxuexi.tools.MyLog;
import net.bcxuexi.tools.RingByteBuffer;
import net.bcxuexi.tools.StringUtils;

/**
 * 读取socket输入流
 */
public class ReadStream extends Thread {
	private SocketModel socketModel;
	private boolean stopRead = false;

	// 输入流数据处理器
	private ReadStreamHandler readStreamHandler;

	public ReadStream(SocketModel socketModel) {
		this.socketModel = socketModel;
		readStreamHandler = new ReadStreamHandler(this);
		readStreamHandler.start();
	}

	@Override
	public void run() {
		try {
			Socket socket = socketModel.getSocket();

			if (socket == null || socket.isClosed()) {
				stopRead = true;
			}

			/*
			 * 1.读取输入流数据 2.把数据交给处理类去处理
			 */
			InputStream in = socket.getInputStream();
			byte[] buffer = new byte[1024 * 4];
			int total = 0;
			RingByteBuffer ringByteBuffer = new RingByteBuffer(1024 * 20);
			int notReadFullNum = 0;//连续未取到完整数据包次数
			while (!stopRead) {
				total = in.read(buffer);
				if (total == -1) {
					MyLog.info("readstream read结果为-1");
					break;
				}
				// 分析数据，
				// 如果是代理数据直接处理
				if (Config.SocketType.proxy.equals(socketModel.getType())) {
					StreamData streamData = new StreamData();
					streamData.setData(buffer);
					streamData.setSocketModel(socketModel);
					streamData.setTotal(total);
					readStreamHandler.addData(streamData);
				} else {
					try {
						//String msgT = new String(buffer,0,total);
						// 数据写入缓存
						ringByteBuffer.put(buffer, 0, total);
						/*
						 * 取一个消息包的数据
						 */

						byte[] msg = ringByteBuffer.getS(Config.start,
								Config.end);
						if (msg != null) {
							notReadFullNum=0;
							//去掉开始和结束标志
							byte[] newMsg = new byte[msg.length-Config.start.length-Config.end.length];
							for(int i=0;i<newMsg.length;i++){
								newMsg[i] = msg[Config.start.length+i];
							}
							StreamData streamData = new StreamData();
							streamData.setData(newMsg);
							streamData.setSocketModel(socketModel);
							streamData.setTotal(newMsg.length);
							readStreamHandler.addData(streamData);
						}else{
							MyLog.info("未取到完整数据包.次数="+notReadFullNum+". "+socketModel.toString());
							notReadFullNum++;
						}
					} catch (MyException my) {
						MyLog.info(my.msg);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyLog.info("readstream结束"+ socketModel.toString());
			readStreamHandler.stopHandler();
		}

	}

	public void stopRead() {
		stopRead = true;
	}
}
